package com.cangsan.erhai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.CreateOrderReq;
import com.cangsan.erhai.entity.SysUser;
import com.cangsan.erhai.entity.TabOrder;
import com.cangsan.erhai.service.SysUserService;
import com.cangsan.erhai.service.TabOrderService;
import com.cangsan.erhai.service.impl.WechatPayService;
import com.cangsan.erhai.util.SecurityUtil;
import com.cangsan.erhai.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/order")
public class TabOrderController {
    @Autowired
    private TabOrderService orderService;
    @Autowired
    private WechatPayService wechatPayService;
    @Autowired
    private SysUserService sysUserService;


    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<TabOrder> queryWrapper = new QueryWrapper<>();
        JSONObject param = pageBean.getParam();
        String phonenumber = param.getString("phonenumber");
        if(StringUtil.isNotEmpty(phonenumber)){
            queryWrapper.eq("phonenumber",phonenumber);
        }
        String nickName = param.getString("nickName");
        if(StringUtil.isNotEmpty(nickName)){
            queryWrapper.like("nick_name",nickName);
        }
        String outTradeNo = param.getString("outTradeNo");
        if(StringUtil.isNotEmpty(outTradeNo)){
            queryWrapper.eq("out_trade_no",outTradeNo);
        }
        String transactionId = param.getString("transactionId");
        if(StringUtil.isNotEmpty(transactionId)){
            queryWrapper.eq("transaction_id",transactionId);
        }

        String userId = param.getString("userId");
        if(StringUtil.isNotEmpty(userId)){
            SysUser user = sysUserService.getById(userId);
            queryWrapper.eq("open_id",user.getOpenId());
        }

        queryWrapper.orderByDesc( "create_time","pay_time");
        Page<TabOrder> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<TabOrder> pageResult = orderService.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        List<TabOrder> records = pageResult.getRecords();
        result.put("dataList",records);
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }
    @PostMapping("/createOrder")
    public R createOrder(@RequestBody CreateOrderReq createOrderReq) throws Exception {
        return wechatPayService.createOrder(createOrderReq);
    }

    @PostMapping("/tefund")
    public R tefund(@RequestBody JSONObject param) throws Exception {
        String outTradeNo = param.getString("outTradeNo");
        Long totalAmount = param.getLong("totalAmount");
        if(StringUtil.isEmpty(outTradeNo)){
            return R.fail("订单号空错误");
        }
        if(Objects.isNull(totalAmount)){
            return R.fail("订单金额空错误");
        }
        return wechatPayService.createRefund(outTradeNo,totalAmount);
    }

    @PostMapping("/payrooback")
    public String payrooback(HttpServletRequest request) throws Exception {
        return wechatPayService.payNotify(request);
    }
}
